// Copyright (c) 2024, lanlian and contributors
// For license information, please see license.txt

frappe.ui.form.on("EMS Acceptance Settlement", {
	refresh(frm) {
        // 生成k3cloud其他应付单
		if (frm.doc.docstatus === 1 && !frm.doc.erp_billno && frm.doc.supplier) {
			frm.add_custom_button(__('Sync To K3cloud AP Otherpayable'), () => {
				frappe.call({
					method: "ems.equipment_managerment_system.doctype.server_config.server_config.sync_to_k3cloud_ap_otherpayable",
					args: {docname: frm.doc.name},
					freeze_message: __("Creating Task ..."),
					callback: (r) => {
						frappe.model.sync(r.message);
						if (r.message) {
								console.log(r.message);
							// frappe.db.set_value(frm.doctype, frm.docname, {'erp_billno': r.message}).then(r => {frappe.msgprint(`同步ERP成功, 单号: ${r.message}`)})
						}
					},
				});
			});
		}
		else if (frm.doc.erp_billno) {frappe.msgprint(`已经同步ERP, 单号: ${frm.doc.erp_billno}`)}
	},
    labor_cost: function(frm) {
        frm.set_value('total_cost', frm.doc.labor_cost + frm.doc.material_cost + frm.doc.other_cost);
        frm.refresh_field('material_cost');
    },
    material_cost: function(frm) {
        if (frm.doc.parts_item.length === 0) {
            frm.set_value('total_cost', frm.doc.labor_cost + frm.doc.material_cost + frm.doc.other_cost);
            frm.refresh_field('material_cost');
        }
    },
    other_cost: function(frm) {
        frm.set_value('total_cost', frm.doc.labor_cost + frm.doc.material_cost + frm.doc.other_cost);
        frm.refresh_field('material_cost');
    },
});
// 子表items部分的代码: 自动计算[total_cost]
frappe.ui.form.on('EMS Settlement Item', {
	// 数量改变时触发
	qty(frm, cdt, cdn) {
		const row = locals[cdt][cdn];
		row.amount_no_tax = (1 * row.qty) * (1 * row.price_no_tax);
		var material_cost = 0;
		$.each(frm.doc.parts_item, function(i, d) { material_cost += (1 * d.qty) * (1 * d.price_no_tax); });
        console.log(material_cost);
		frm.set_value('material_cost', material_cost);
        frm.set_value('total_cost', frm.doc.labor_cost + material_cost + frm.doc.other_cost);
		frm.refresh_field('material_cost');
        frm.refresh_field('total_cost');
		frm.refresh_field('parts_item');
	},
	// 数量改变时触发
	price_no_tax(frm, cdt, cdn) {
		const row = locals[cdt][cdn];
		row.amount_no_tax = (1 * row.qty) * (1 * row.price_no_tax);
		var material_cost = 0;
		$.each(frm.doc.parts_item, function(i, d) { material_cost += (1 * d.qty) * (1 * d.price_no_tax);});
        console.log(material_cost);
		frm.set_value('material_cost', material_cost);
        frm.set_value('total_cost', frm.doc.labor_cost + material_cost + frm.doc.other_cost);
		frm.refresh_field('material_cost');
		frm.refresh_field('total_cost');
		frm.refresh_field('parts_item');
	},
    // 删除分录时触发
    parts_item_remove(frm, cdt, cdn) {
		var material_cost = 0;
		$.each(frm.doc.parts_item, function(i, d) { material_cost += (1 * d.qty) * (1 * d.price_no_tax);});
        console.log(material_cost);
		frm.set_value('material_cost', material_cost);
        frm.set_value('total_cost', frm.doc.labor_cost + material_cost + frm.doc.other_cost);
		frm.refresh_field('material_cost');
		frm.refresh_field('total_cost');
		frm.refresh_field('parts_item');
    },
});
